home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / insert.pas < prev    next >
Pascal/Delphi Source File  |  1993-03-04  |  4KB  |  128 lines

  1.  
  2. program InsertRedrawDemo;
  3. {demonstrates reading an input line in a dialog box, }
  4. {placing an output in another input line in the      }
  5. {same box while box stays open.                      }
  6. {Author: Stewart Midwinter, with thanks to           }
  7. {Blake Watson [70303,373] for his assistance.        }
  8.  
  9. uses Objects,Drivers,Views,Menus,Dialogs,App,Crt;
  10.  
  11. const
  12.   cmNewDialog = 101;
  13.   cmConvert   = 55555;
  14.  
  15. type
  16.   PDataRec = ^DataRec;
  17.   DataRec  = RECORD
  18.    Field1: string [ 15 ] ; 
  19.    Field2: string [ 15 ] ; 
  20.   END ;
  21.  
  22. const
  23.   DemoDialogData: DataRec = (Field1: '123.45'; Field2: '');
  24.  
  25. type
  26.   TMyApp = object(TApplication)
  27.     procedure HandleEvent(var Event: TEvent); virtual;
  28.     procedure InitStatusLine; virtual;
  29.     procedure NewDialog;
  30.   end;
  31.  
  32.   PDemoDialog = ^TDemoDialog;
  33.   TDemoDialog = object(TDialog)
  34.          POutput: PInputLine;
  35.           PInput: PInputLine;
  36.   procedure HandleEvent(var Event:TEvent); virtual;
  37.   end;
  38. { pointers to the inputlines (which could be descendant types for
  39.   validation) are needed here so that HandleEvent can work with the data}
  40. { Box needs its own HandleEvent since we are defining additional behaviour}
  41.  
  42. procedure TMyApp.HandleEvent(var Event: TEvent);
  43. begin
  44.       TApplication.HandleEvent(Event);
  45.       if Event.What = evCommand then
  46.       begin
  47.         case Event.Command of
  48.         cmNewDialog: NewDialog;
  49.         else Exit;
  50.       end;
  51.       ClearEvent(Event);
  52.       end;
  53. end;
  54.  
  55. procedure TMyApp.InitStatusLine;
  56. var R: TRect;
  57. begin
  58.       GetExtent(R);
  59.       R.A.Y := R.B.Y - 1;
  60.       StatusLine := New(PStatusLine, Init(R,
  61.       NewStatusDef(0,$FFFF,NewStatusKey('~Alt-X~ Exit',kbAltX,cmQuit,
  62.       NewStatusKey('~F2~ Dialog',kbF2,cmNewDialog,nil)),nil)));
  63. end;
  64.   {you don't need a menubar to create a dialog box}
  65.   
  66. procedure TMyApp.NewDialog;
  67. var Dlg: PDemoDialog;
  68.     R: TRect; C: word;
  69.     IControl: PInputline; {could be PTimeInputLine or whatever}
  70.     OControl: PInputline;
  71.     
  72. begin
  73.    R.Assign(3,1,44,13);
  74.    Dlg:=New(PDemoDialog,Init(R,'Input/Output'));
  75. with Dlg^ do begin
  76.    R.Assign ( 12 , 2 , 37 , 3 ) ;
  77.    IControl:= New ( PInputline , Init ( R , 15 ));
  78.    Dlg^.Insert ( IControl ) ;
  79.    Dlg^.PInput := IControl;
  80.    R.Assign ( 5 , 2 , 12 , 3 ) ;
  81.    Dlg^.Insert (New(PLabel,Init(R,'~I~nput',IControl))) ;
  82.    R.Assign ( 12 , 4 , 37 , 5 ) ;
  83.    OControl:= New(PInputline,Init(R,15));
  84.    Dlg^.Insert (OCOntrol);
  85.    Dlg^.POutput := OCOntrol;
  86.    R.Assign ( 4 , 4 , 12 , 5 ) ;
  87.    Dlg^.Insert (New(PLabel,Init(R,'~O~utput',OControl ))) ;
  88.    R.Assign ( 7 , 6 , 18 , 8 ) ;
  89.    Dlg^.Insert(New(PButton,Init (R,'Convert~',cmConvert,bfDefault))) ;
  90.    R.Assign ( 20 , 6 , 30 , 8 ) ;
  91.    Dlg^.Insert(New(PButton,Init(R,'Done',cmOk,0)));
  92.                              {note OK button has no bf flag}
  93.    Dlg^.SelectNext(False);
  94.    Dlg^.SetData(DemodialogData);
  95.          {omit if you don't need initial data shown upon opening the box}
  96. end;
  97.   C := DeskTop^.ExecView(Dlg);
  98.          {don't need to Insert box to have it remain open while we
  99.           undertake conversions.}
  100. end;
  101.  
  102. procedure TDemoDialog.HandleEvent;
  103. var S: string;
  104. begin TDialog.HandleEvent(Event);
  105.       if Event.What = evCommand then begin
  106.       Case Event.Command of
  107.       cmConvert: begin
  108.                  sound(2000); delay(10); nosound;
  109.                  S:= PInput^.Data^;    {string contains contents of input-
  110.                                         line pointed to by PInput }
  111.                  PInput^.GetData(S);   {GetData is only necessary here if
  112.                                         you are overriding the default, eg.
  113.                                         converting km to miles, etc.}
  114.  
  115.                  POutput^.SetData(S);
  116.                  end;
  117.       end;
  118.       end;
  119. end;
  120.  
  121. var   MyApp: TMyApp;
  122.  
  123. begin
  124.     MyApp.Init;
  125.     MyApp.Run;
  126.     MyApp.Done;
  127. end.
  128.